var msgdsq;
//错误时：提示调用方法
function show_err_msg(msg){
    $('.msg_bg').html('');
    clearTimeout(msgdsq);
    $('body').append('<div class="sub_err" style="position:absolute;top:60px;left:0;width:500px;z-index:999999;"></div>');
    var errhtml='<div style="padding:8px 0px;border:1px solid #ff0000;width:100%;margin:0 auto;background-color:#fff;color:#B90802;border:3px #ff0000 solid;text-align:center;font-size:16px;font-family:微软雅黑;"><img style="margin-right:10px;" src="../assets/img/error.png">';
    var errhtmlfoot='</div>';
    $('.msg_bg').height($(document).height());
    $('.sub_err').html(errhtml+msg+errhtmlfoot);
    var left=($(document).width()-500)/2;
    $('.sub_err').css({'left':left+'px'});
    var scroll_height=$(document).scrollTop();
    $('.sub_err').animate({'top': scroll_height+120},300);
    msgdsq=setTimeout(function(){
        $('.sub_err').animate({'top': scroll_height+80},300);
        setTimeout(function(){
            $('.msg_bg').remove();
            $('.sub_err').remove();
        },300);
    }, "1000");
}

//正确时：提示调用方法
function show_msg(msg,url){
    $('.msg_bg').html('');
    clearTimeout(msgdsq);
    $('body').append('<div class="sub_err" style="position:absolute;top:60px;left:0;width:500px;z-index:999999;"></div>');
    var htmltop='<div style="padding:8px 0px;border:1px solid #090;width:100%;margin:0 auto;background-color:#FFF2F8;color:#090;border:3px #090 solid;;text-align:center;font-size:16px;"><img style="margin-right:10px;" src="../assets/img/success.png">';
    var htmlfoot='</div>';
    $('.msg_bg').height($(document).height());
    var left=($(document).width()-500)/2;
    $('.sub_err').css({'left':left+'px'});
    $('.sub_err').html(htmltop+msg+htmlfoot);
    var scroll_height=$(document).scrollTop();
    $('.sub_err').animate({'top': scroll_height+120},500);
    msgdsq=setTimeout(function(){
        $('.sub_err').animate({'top': scroll_height+80},500);
        setTimeout(function(){
            $('.msg_bg').remove();
            $('.sub_err').remove();
            if(url!='')
            {
                location.href=url;
            }
        },800);

    }, "1200");
}

//显示加载动画
function show_loading()
{
    var str='<div class="msg_bg" style="background:#000;opacity:0.5;filter:alpha(opacity=50);z-index:99998;width:100%;position:absolute;left:0;top:0"></div>';
    str+='<div class="msg_bg" style="z-index:99999;width:100%;position:absolute;left:0;top:0;text-align:center;"><img src="../assets/img/loading.gif" alt="" class="loading"></div>'
    $('body').append(str);
    var scroll_height=$(document).scrollTop();
    $('.msg_bg').height($(document).height());
    $('.loading').css('margin-top',scroll_height+240);
}
//支持键盘Enter键登录
document.onkeydown = function(e){
    if(!e) e = window.event;
    if((e.keyCode || e.which) == 13){
        var obtnLogin=document.getElementById("login");
        obtnLogin.click();
    }
}
$(function () {
    $("#login").click(function () {
        if(validateNULL($("#username"))&&validateNULL($("#password"))){
            // ajax("http://localhost:8080/user/login.action",)
            // console.log( JSON.stringify({
            //     "username": $("#username").val(),
            //     "password": $("#password").val()
            // }));
            ajax("http://localhost:8080/user/login.action",
                "username="+$("#username").val()+
                "&password="+ $("#password").val()
                , login, error);
            // ajax("http://localhost:8080/user/login.action", JSON.stringify({
            //     "username": $("#username").val(),
            //     "password": $("#password").val()
            // }), login, error);
            $("#login").html('wait <i class="am-icon-spinner am-icon-pulse"></i>');
        }
    });
    function login(data) {
        console.info(data);
        // console.info(data.data);
        var userInfo=data.data;
        if (data.code == 500) {
            // $("#login").html('<i class="am-icon-times"></i>账号或密码错误！');
            // $("#login").attr("class", "am-btn am-btn-lg am-fl am-btn-block am-btn-danger");
            show_err_msg("密码错误，请输入正确的账号密码");
            setTimeout(function () {
                $("#login").html("登 录");
                $("#login").attr("class", "am-btn am-btn-primary am-btn-lg am-fl am-btn-block");
            }, 3000);
        } else {
            // sessionStorage.username = data.adminUser.username;
            // location.href = "admin/index.action";
            // alert("该跳转了")
            show_msg('登录成功咯！  正在为您跳转...','/');
        }
    }
    //验证输入的表单的元素是否为空，为空则返回false
    function validateNULL(obj) {
        if(obj.val()==""){
            obj.parent().addClass("am-form-warning");
            nullMsg(obj);
            return false;
        }
        return true;
    }
    //输出不能为空的错误信息
    function nullMsg(obj){
        obj.attr("placeholder",obj.attr('id')+"不能为空");
    }

    //ajax封装
    function ajax(url, data, success, error) {
        // alert(data);
        $.ajax({
            type: 'post',
            data: data,
            url: url,
            // dataType: 'json',
            // contentType: "application/json",
            success: function (d) {
                success(d);
            },
            error: function (e) {
                error(e);
            }
        });
    }
    function error(e) {
        console.error(e);
        // alert("服务器错误！");
        show_err_msg("服务器错误!");
    }


});
